// b2c/login/login.js
var RSAKey = require('../../js/rsa.js');
const app = getApp();
const url_path = app.globalData.url;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    yzm:"发送验证码",
    iphone:null,
    code:null,
    cla:'logs-ver',
    all:{
      countdown:60,
      timecoun:60
    },
    tier:true,
    n:32
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {   
    
  },
  logsVert:function(e){
    if (this.data.tier) {
      if (!(/^1[12345789]\d{9}$/).test(this.data.iphone)) {
        wx.showToast({
          title: '请输入合法的手机号码',
          icon: 'success',
          duration: 1000
        })
        return false;
      }
      this.setTime();
      var dataJson = {
        S: this.data.iphone
      }
      var header = {
        
      }
      app.aeHttp(
        {
          method: "POST",//请求类型
          api: `${url_path}xcxm_wx`,//路由地址
          header: header,//头部参数设置 - 设置编码 或 获取cookie 。。。
          data: dataJson//存放要传给后端借口的参数集合
        },
        function (res) {
          //success-成功
          if (res == "0") {
            wx.showToast({
              title: '发送成功!',
              icon: 'success',
              duration: 1000
            })
          } else {
            wx.showToast({
              title: '发送失败!',
              icon: 'success',
              duration: 1000
            })
          }
        },
        function (err) {
          //error-失败
          wx.showToast({
            title: '短息发送出错',
            icon: 'success',
            duration: 1000
          })
          return ;
        })
    }
    
  },
  logins:function(){
    var reg = new RegExp('')
    if(!(/^1[12345789]\d{9}$/).test(this.data.iphone)){
      wx.showToast({
        title: '请输入合法的手机号码',
        icon: 'success',
        duration: 1000
      })
      return false;
    }
    if (!(/^[0-9]{6}$/).test(this.data.code)){
      wx.showToast({
        title: '请输入正确的验证码',
        icon: 'success',
        duration: 1000
      })
      return false;
    }
    wx.request({
      url: 'https://csb2c.fenxiangqu.cn/xcx10002_wx',
      data: {
        n: this.data.n,
      },
      method: "POST",
      success: (res) => {
        var html = res.data,
        str = html.split(',');
        var rsa = new RSAKey.RSAKey();
        rsa.setPublic(str[0], "3");
        wx.request({
          url: 'https://csb2c.fenxiangqu.cn/xcxlogin',
          data:{
            u: RSAKey.linebrk(rsa.encrypt(this.data.iphone),64),
            p: RSAKey.linebrk(rsa.encrypt(this.data.code), 64),
            r: str[1]
          },
          method: "POST",
          success: (res) =>{
            console.log(res.data)
            if (res.data.ret == "1" || res.data.ret == "2"){
              
              try {
                wx.setStorageSync('cookie',res.data.cookie);
                wx.showToast({
                  title: '登录成功',
                  icon: 'success',
                  duration: 1000
                })
                wx.switchTab({
                  url: '../center/center'
                })
                
              } catch (e) {
                console.log
              }
              return false;
            }     
          },
          fail:(res) =>{
            wx.showToast({
              title: '登录出错！',
              icon: 'success',
              duration: 1000
            })
            return false;
          }
        })

      },
      fail: (res) => {
        wx.showToast({
          title: '请求出错',
          icon: 'success',
          duration: 1000
        })
        return false;
      }
    })
    
  },
  bindReplaceInput:function(e){
    this.setData({
      iphone: e.detail.value
    })
  },
  bindcode:function(e){
    this.setData({
      code:e.detail.value
    })
  },
  setTime:function(){
    var self = this;
    if (this.data.all.countdown == 0){
      this.setData({
        yzm:"发送验证码",
        cla:"logs-ver",
        tier:true,
        all: {
          countdown:60
        }
      })
      clearTimeout(self.setTime);
    }else{
      var num = this.data.all.countdown-1;
      this.setData({
        yzm: "重新发送(" + this.data.all.countdown +")",
        cla:"logs-back",
        all:{
          countdown: num
        },
        tier:false
      })
      setTimeout(function(){ self.setTime() },1000)
    }
  },
  time:function(){
    var self = this;
    if (self.data.all.timecoun == 0){
      this.setData({
        yzm: "发送验证码",
        cla: "logs-ver",
        tier: true,
        all: {
          timecoun: 60
        }
      })
    }else{
      var num = this.data.all.timecoun - 1;
      this.setData({
        yzm: "重新发送(" + this.data.all.timecoun + ")",
        cla: "logs-back",
        all: {
          timecoun: num
        },
        tier: false
      })
      setTimeout(function () { self.time() }, 1000)
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})